var tableVm=new Vue({
	el: '#table-manager',
  	beforeCompile:function(){
		$("body").LoadingOverlay("show",{
	  		fade : [500, 200]
		});
  	},
  	ready:function(){
  		//处理页码
  		this.page=laypage.handlePage();
  		//填充数据
  		this.handleContent();
  	},
  	data:{
  		page:0,
  		size:10,
  		sort:"created,desc",
  		searchName:null,
  		searchType:'',
  		info:{},
  		selInfo:{},
  		initLaypage:false,
  		content:null,
  		isAdd:true,
  		dicType:{"01":"普通","02":"包间"}
  	},
  	methods:{
  		addRecord:function(){
  			this.info={"type":"01"};
  			this.isAdd=true;
  			$("#table-modal").modal("show");
  		},
  		editRecord:function(info){
  			this.selInfo=info;
  			this.info=_.mapKeys(info, function(value, key) {
  			  return key.toLowerCase();
  			});
  			this.isAdd=false;
  			$("#table-modal").modal("show");
  		},
  		saveRecord:function(){
  			if(this.isAdd){
	  			this.$http.post("table/oper",this.info).then((response) => {
	  				$("#table-modal").modal("hide");
	  				this.reloadContent(0);
		  		}, (response) => {
		  			$.alt.message("提示",response.body);
		  		});
  			}else{
  				this.$http.put("table/oper",this.info).then((response) => {
  					this.selInfo.NUM=this.info.num;
  					this.selInfo.TYPE=this.info.type;
  					this.selInfo.DESCR=this.info.descr;
  					$("#table-modal").modal("hide");
		  		}, (response) => {
		  			$.alt.message("提示",response.body);
		  		});
  			}
  		},
  		delRecord:function(id){
  			$.alt.confirm("提示","您确认要删除该记录吗？",function(){
  				this.$http.delete("table/oper",{params:{id:id}}).then((response) => {
  					if(!this.content || this.content.length==1){
		  				this.reloadContent(0);
	  				}else{
		  				this.reloadContent(this.page);
	  				}
		  		}, (response) => {
		  			$.alt.message("提示",response.body);
		  		});
  				return true;
  			}.bind(this));
  		},
  		reloadContent:function(page){
  			$("body").LoadingOverlay("show",{
		  		fade : [500, 200]
			});
  			this.page=page;
			this.initLaypage=false;
			this.handleContent();
  		},
  		handleContent:function(){
  			this.$http.post("table/list",{
  				page:this.page,
  				size:this.size,
  				sort:this.sort,
  				searchName:this.searchName,
  				searchType:this.searchType
	  		},{emulateJSON : true}).then((response) => {
	  			this.content=response.data['content'];
	  			if(!this.initLaypage){
		  			//分页初始化
		  			laypage({
		  			    cont: 'laypage-ctl',
		  			    skin: 'molv',
		  			    pages: response.data['totalPages'],
		  			    curr: this.page+1,
		  			    //hash: 'page',
		  			    jump: function(o){
		  			    	if(this.initLaypage){
		  			    		$("body").LoadingOverlay("show",{
		  					  		fade : [300, 200]
		  						});
		  			    		this.page=o.curr-1;
			  			    	this.handleContent();
		  			    	}
		  			    }.bind(this)
		  			});
		  			this.initLaypage=true;
	  			}
	  			$("body").LoadingOverlay("hide", true);
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		}
  	}
});